﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Microsoft.Popfly.GameCreator.GameEngine.Behaviors
{
    public class ShootBehavior : IBehavior
    {
        private Actor m_shooter;
        private string m_actorName;
        private Game m_game;

        private Data.MotionInfo m_motion;

        public ShootBehavior(Game game, BehaviorOwner shooter, Data.BulletBehaviorInfo behaviorInfo)
        {            
            Init(game, shooter, behaviorInfo.ActorReference.Name, behaviorInfo.Motion);

        }

        public void Init(Game game, BehaviorOwner shooter, string actorName, Data.MotionInfo motion)
        {
            if (shooter.IsActor == false)
            {
                throw new ArgumentException("You can only add shoot behaviors to actors");
            }
            m_game = game;
            m_shooter = (Actor) shooter;
            m_actorName = actorName;
            m_motion = motion;
        }
        #region IBehavior Members

        public void Invoke(BehaviorOwner sender, Microsoft.Popfly.GameCreator.GameEngine.Events.PGCEventArgs args)
        {
            Actor projectile = m_shooter.Scene.AddActor(m_shooter, m_actorName);

            if (m_motion != null && m_motion.Direction != Data.MotionInfo.DirectionValues.NotDefined)
            {
                Data.MotionBehaviorInfo motionBehaviorInfo = new Data.MotionBehaviorInfo();

                motionBehaviorInfo.Motion = m_motion;
                motionBehaviorInfo.Event = new Data.LoadEventInfo();
                motionBehaviorInfo.Event.Type = "Load";
                motionBehaviorInfo.BehaviorType = "motion";

                try
                {
                    Events.StandardListener listener = new Events.StandardListener(m_game, projectile, motionBehaviorInfo);
                    projectile.AddBehavior(listener);
                }
                catch (Exception e)
                {
                    Console.ReportError(
                        "Error creating motion behavior to shoot actor '" +
                        projectile.Name +
                        "' from actor '" + m_shooter.Name + "'.\n" +
                        "Message: '" + e.Message + "'\n" +
                        "Exception: \n" + e.ToString() + "\n");
                }
            }
        }

        #endregion
    }
}
